package com.labichaoka.chaoka.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

import com.bumptech.glide.Glide;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

/**
 * Created by ruan on 2018/6/1.
 */

public class ShareUtils {

    public boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    public void share(String shareType, String title, String description, String webUrl, String imgUrl, Activity act) {
        ShareAction mShareAction = new ShareAction(act).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setShareboardclickCallback((snsPlatform, share_media) -> {
                    switch (shareType) {
                        case "0":
                            //分享纯文本
                            new ShareAction(act).withText(title)
                                    .setPlatform(share_media)
                                    .share();
                            break;
                        case "1":
                            //分享纯网络图片
                            ThreadPoolManager.getInstance().execute(() -> {
                                try {
                                    Bitmap bitmap = Glide.with(act).load(imgUrl).asBitmap().centerCrop().into(100, 100).get();
                                    UMImage imageurl = new UMImage(act, imgUrl);
                                    imageurl.setThumb(new UMImage(act, bitmap));
                                    new ShareAction(act).withMedia(imageurl)
                                            .setPlatform(share_media)
                                            .share();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            });
                            break;
                        case "2":
                            //分享文字链接
                            ThreadPoolManager.getInstance().execute(() -> {
                                try {
                                    Bitmap bitmap = Glide.with(act).load(imgUrl).asBitmap().centerCrop().into(100, 100).get();
                                    UMWeb web = new UMWeb(webUrl);
                                    web.setTitle(title);
                                    web.setDescription(description);
                                    web.setThumb(new UMImage(act, bitmap));
                                    new ShareAction(act).withMedia(web)
                                            .setPlatform(share_media)
                                            .share();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            });
                            break;
                    }
                });
        mShareAction.open();
    }


}
